package com.cskaoyan.service.admin;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseDataListVo;
import com.cskaoyan.model.admin.vo.OrderDetailVo;
import com.cskaoyan.model.admin.vo.OrderReplyVo;
import com.cskaoyan.model.admin.vo.UserOrderVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/11/30 22:09
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public OrderDetailVo orderDetail(String sid) {
        Integer id;
        try {
            id = Integer.parseInt(sid);
        } catch (Exception e) {
            throw new IllegalArgumentException();
        }
        MarketOrderExample example = new MarketOrderExample();
        example.createCriteria().andIdEqualTo(id);
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        MarketOrderGoodsExample example1 = new MarketOrderGoodsExample();
        example1.createCriteria().andGoodsSnEqualTo(id.toString());
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example1);
        MarketUserExample example2 = new MarketUserExample();
        example2.createCriteria().andIdEqualTo(marketOrders.get(0).getUserId());
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example2);

        UserOrderVo userOrderVo = new UserOrderVo();
        userOrderVo.setAvatar(marketUsers.get(0).getAvatar());
        userOrderVo.setNickname(marketUsers.get(0).getNickname());
        OrderDetailVo orderDetailVo = new OrderDetailVo();

        orderDetailVo.setOrder(marketOrders.get(0));
        orderDetailVo.setOrderGoods(marketOrderGoods);
        orderDetailVo.setUser(userOrderVo);

        return orderDetailVo;
    }

    @Override
    public BaseDataListVo orderList(BaseParam baseParam, String[] timeArray, String orderSn, String userId, Short[] orderStatusArray) throws ParseException {
        int id = 0;
        try {
            if (userId != null && userId.length() > 0) {
                id = Integer.parseInt(userId);
            }
        } catch (Exception e) {
            return new BaseDataListVo();
        }
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        if (orderSn != null && orderSn.length() > 0) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (userId != null && userId.length() > 0) {
            criteria.andUserIdEqualTo(id);
        }
        if (timeArray != null && timeArray.length != 0) {
            criteria.andAddTimeGreaterThan(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(timeArray[0])).andEndTimeLessThan(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(timeArray[1]));
        }
        if (orderStatusArray != null && orderStatusArray.length > 0) {
            for (Short orderStatus : orderStatusArray) {
                criteria.andOrderStatusEqualTo(orderStatus);
            }
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketOrders);

        BaseDataListVo baseDataListVo = new BaseDataListVo();
        baseDataListVo.setList(marketOrders);
        baseDataListVo.setPage(baseParam.getPage());
        baseDataListVo.setTotal((int) pageInfo.getTotal());
        baseDataListVo.setLimit(baseParam.getLimit());
        baseDataListVo.setPages(pageInfo.getPages());
        return baseDataListVo;
    }

    @Override
    public void orderReply(OrderReplyVo orderReplyVo) {
        MarketCommentExample example = new MarketCommentExample();
        example.createCriteria().andIdEqualTo(orderReplyVo.getCommentId());
        MarketComment marketComment = marketCommentMapper.selectByPrimaryKey(orderReplyVo.getCommentId());
        if (!StringUtils.isEmpty(marketComment.getAdminContent())) {
            throw new IllegalArgumentException();
        }
        marketComment.setUpdateTime(new Date());
        marketComment.setAdminContent(orderReplyVo.getContent());
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);
    }

    @Override
    public void orderDelete(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setUpdateTime(new Date());
        marketOrder.setDeleted(true);
        marketOrder.setId(orderId);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }
}
